close all
clear all

[fname,fpath]=uigetfile('*.csv','select the clean file to open');
rawData=dlmread(strcat(fpath,fname),',');
%single fish have nan in school id from labview cleaning, exploit this to
%find single fish
indSingle=find(isnan(rawData(:,11)));

%pull out the single fish
rawSingleFish=rawData(indSingle,:);

%now, create a schooling fish array, and remove the singles.
superSchoolData=rawData;
superSchoolData(indSingle,:)=[];
%at this point the single and schooling detections should be seperate.


allIds=unique(rawSingleFish(:,8)); %note that ID of zero is no id, e.g., some school stuff
%now loop through the school ids and process them into the main singles
%array
%clean zeros out of the allIds
badZind=find(allIds==0);
allIds(badZind)=[];





%get all unique school Ids
% allSchools is an array of unique school id #s
allSchoolIDs=unique(superSchoolData(:,11)); %should contain school ids
crapInd=find(isnan(allSchoolIDs));
allSchoolIDs(crapInd,:)=[]; %cut out the nans


%now work through the school adding fish and synthetic fish to the single
%fish array

totSchooling=0; 

%we need to create new IDs for the fish comming from the superSchoolsArray,
%so find the largest id of the single fish, then count up from there
%createing a new ID for every fish moved in from the schools array
maxFish=max(allIds);
newFish=maxFish+1; %starting id for new fish added
startingUniqueRaw=size(allIds)

clear allIds badZind

for i=1:length(allSchoolIDs) %loop through the school ids
    
    thisSuperSchool=[]; %empty the super school array
    
    
    % superSchoolData is the raw data containing super school IDs
    % allSchoolIDs is an array of unique school id #s
    % indOfThisSchool is a vector of indicies of the raw data that include
    %               entries for this superschool id
    % thisSuperSchool is the an array that contains the rows of this
    %               school's data extracted from the main array
    
    %find the indicies of this super school in the data arrray
    indOfThisSchool=find(superSchoolData(:,11)==allSchoolIDs(i));
    
    %create a new array just for lines of data from this super school
    thisSuperSchool=superSchoolData(indOfThisSchool,:);
    
    %now, delete this schools data from the raw data array
    superSchoolData(indOfThisSchool,:)=[];
    
       
    %find all of the unique fishIDs of fish in the School that are ID'd
    fishNamedInSchool=unique(thisSuperSchool(:,8));
    badInd=find(fishNamedInSchool==0 | isnan(fishNamedInSchool));
    fishNamedInSchool(badInd,:)=[];
    
    %find out how many fish in the school were called out by ID
    numberNamedFish=length(fishNamedInSchool);
    %calculate how many fish need to be added to the individual fish array
    %for this fish.
    nToMake=max([numberNamedFish,max(thisSuperSchool(:,12))]);
    
    totSchooling=totSchooling+nToMake; %update the totSchooling count
    % this is just a housekeeping variable to keep track of how many are
    % made during school disagregation
    
    %extract the data used to make up the synthetic fish
    synthDataRow=thisSuperSchool(1,:); %use the first row of data as the 
    % basis for all the schools fish data
    
    %Give the sythetic fish the best (min) score of the school
    synthDataRow(1,10)=min(thisSuperSchool(:,10));
    %Give the synthetic fish the greatest size of the school
    synthDataRow(1,9)=max(thisSuperSchool(:,9));
    synthDataRow(1,19)=3; %starting code, which means that this is a
    %synthetic fish from a super school with no ID
    
    %loop through and remove the school fish with IDs called out
    for j=1:length(fishNamedInSchool) 
        thisNamedSchoolFish=find(thisSuperSchool(:,8)==fishNamedInSchool(j));
        if isempty(thisNamedSchoolFish)
            dataRow=synthDataRow;
        else
            dataRow=thisSuperSchool(thisNamedSchoolFish(1),:);
        end
        dataRow(1,9)=max(thisSuperSchool(thisNamedSchoolFish,9)); %fill in the max size as the size
        dataRow(1,10)=min(thisSuperSchool(thisNamedSchoolFish,10)); %fill in the min score as the score
        dataRow(1,8)=newFish; %give it a new id
        dataRow(1,19)=2; %give it a code of 2 to indicate that it came
        %from a super school but was a named individual
        newFish=newFish+1; %count up the new fish
        
        rawSingleFish=[rawSingleFish;dataRow];
        nToMake=nToMake-1; %count down the number of fish to make
    end
    
    if nToMake>0
        for n=1:nToMake
            synthDataRow(1,8)=newFish;
            newFish=newFish+1; %count up the new fish
            rawSingleFish=[rawSingleFish;synthDataRow];
        end
    end
end

clear i j
    
allIds=unique(rawSingleFish(:,8)); %note that ID of zero is no id, e.g., some school stuff
%now loop through the school ids and process them into the main singles
%array
%clean zeros out of the allIds
badZind=find(allIds==0);
allIds(badZind)=[];

totSchooling=totSchooling
postSchoolSize=size(allIds)

%initialize a summary array for looking @ milling
sumData=zeros(length(allIds),26);

%initialize the fish structure
fish(1:length(allIds))=struct('rawData',zeros(1,19),'size',0,'score',0,'year',0,...
    'code',0,'hour',-10,'gstart',0,'gend',0,'totFrames',0,'frames',[0,0]);
k=1; %index for the fish structured array

for i=1:length(allIds)
    id=allIds(i);
    thisIdInds=find(rawSingleFish(:,8)==id);
    %get data on this fish
    fish(k).rawData=rawSingleFish(thisIdInds(1),:);
    fish(k).size=max(rawSingleFish(thisIdInds,9));
    fish(k).score=min(rawSingleFish(thisIdInds,10));
    fish(k).year=rawSingleFish(thisIdInds(1),1);
    fish(k).code=rawSingleFish(thisIdInds(1),19);
    fish(k).hour=rawSingleFish(thisIdInds(1),5);
    %now compile the frames array
    totFrames=0;%initialize the total frames variable
    for j=1:length(thisIdInds)
        fish(k).frames(j,:)=rawSingleFish(thisIdInds(j),16:17);
        totFrames=totFrames+fish(k).frames(j,2)-fish(k).frames(j,1)+1;
        if totFrames<1
            totFrames=1;
        end
    end
    fish(k).gstart=min(fish(k).frames(:,1));
    fish(k).gend=max(fish(k).frames(:,2));
    fish(k).totFrames=totFrames;
    
    sumData(k,1)=fish(k).year;
    sumData(k,2)=id;
    sumData(k,3)=fish(k).size;
    sumData(k,4)=fish(k).totFrames;
    sumData(k,5)=fish(k).score;
    sumData(k,6)=fish(k).code;
    sumData(k,7)=fish(k).hour;
    sumData(k,8:26)=fish(k).rawData;
    k=k+1;
    
end

[fname,fpath]=uiputfile('*.csv','select the summary file to write');
dlmwrite(strcat(fpath,fname),sumData,'delimiter',',','precision','%.12f');

